<template>
  <div>
    <!-- 商品 -->
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区域 -->
    <el-card>
        <el-row >
            <el-col :span="8">
                <el-input placeholder="请输入内容" v-model="queryInfo.query">
                    <el-button slot="append" icon="el-icon-search" @click="getGoodList()"></el-button>
                </el-input>
            </el-col>
            <el-col :span="4">
                <el-button type="primary" @click="goAddpage">添加商品</el-button>
            </el-col>
        </el-row>

        <!-- 表格区域 -->
        <el-table :data="goodList" border stripe>
            <el-table-column type="index"></el-table-column>
            <el-table-column  label='商品名称' prop='goods_name'></el-table-column>
            <el-table-column width="95px" label='商品价格（元）' prop='goods_price'></el-table-column>
            <el-table-column width="70px" label='商品重量' prop='goods_weight'></el-table-column>
            <el-table-column width="140px" label='商品创建时间' prop='add_time'>
                <template slot-scope="scope">
                    {{scope.row.add_time | dataFormat}}
                </template>
            </el-table-column>
            <el-table-column width="130px" label='操作'>
                <template slot-scope="scope">
                <!-- <template> -->
                    <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeById(scope.row.goods_id)"></el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="queryInfo.pagesize"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="400">
    </el-pagination>
  </div>
</template>

<script>
import { instance } from '@/utils/http'
export default {
  data () {
    return {
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 4,
        pagesize: 4
      },
      goodList: [],
      total: 0
    }
  },
  methods: {
    async  getGoodList () {
      const { data: res } = await instance.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.goodList = res.data.goods
      this.total = res.data.total
    //   this.$message.success('获取商品列表成功')
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodList()
    },
    async  removeById (id) {
      const { data: res } = await instance.delete('goods/' + id)
      console.log(res.meta.msg)
    },
    goAddpage () {
      this.$router.push('/goods/add')
    }
  },
  created () {
    this.getGoodList()
  }
}
</script>

<style scoped lang="less">

</style>
